<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="visible = true">新增</el-button>
        <el-button size="small">导出</el-button>
      </template>
    </FilterTable>
    <Drawer
      :visible.sync="visible"
      title="新增整车采购订单"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <OrderPanel @close="closeHandler" />
    </Drawer>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import OrderPanel from './OrderPanel';
import { getOrders } from '@/api/vehicle/order/maintain';

export default {
  name: 'Ordermanage',
  mixins: [dictionary],
  components: {
    OrderPanel
  },
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getOrders,
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns()
    };
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={this.isEdit(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text">
                  明细
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={this.isCommit(props.row)}>
                  提交
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={this.isApproved(props.row)}>
                  特批上报
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '订单编号',
          dataIndex: 'ORDER_NO',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单类型',
          dataIndex: 'ORDER_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4005'),
          render: props => {
            return <span>{this.createDictText(props.row.ORDER_TYPE, '4005')}</span>;
          }
        },
        {
          title: '订单状态',
          dataIndex: 'ORDER_STATUS',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4008'),
          render: props => {
            return <span>{this.createDictText(props.row.ORDER_STATUS, '4008')}</span>;
          }
        },
        {
          title: '特批状态',
          dataIndex: 'SPECIAL_STATUS',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4009'),
          render: props => {
            return <span>{this.createDictText(props.row.SPECIAL_STATUS, '4009')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'FUND_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4007'),
          render: props => {
            return <span>{this.createDictText(props.row.FUND_TYPE, '4007')}</span>;
          }
        },
        {
          title: 'APP订单编号',
          dataIndex: 'APP_ORDER_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'APP下订标识',
          dataIndex: 'IS_APP',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.IS_APP, '1004')}</span>;
          }
        },
        {
          title: '客户名称',
          dataIndex: 'APP_CUSNAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户手机',
          dataIndex: 'APP_CUSTEL',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报数量',
          dataIndex: 'PRODUCT_COUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审批数量',
          dataIndex: 'AUDIT_COUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发货数量',
          dataIndex: 'SHIPMENT_COUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'COMMIT_TIME',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '备注',
          dataIndex: 'REMARK'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '订单编号...'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '订单类型',
          itemList: this.createDictList('4005')
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '订单状态',
          itemList: this.createDictList('4008')
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'fundType',
          placeholder: '资金类型',
          itemList: this.createDictList('4007')
        },
        {
          type: 'RANGE_DATE',
          label: '提交时间',
          style: { minWidth: '200px' },
          fieldName: 'commitStartTime|commitEndTime',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'RANGE_DATE',
          label: '创建时间',
          style: { minWidth: '200px' },
          fieldName: 'createStartTime|createEndTime',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'SELECT',
          label: '下订标识',
          fieldName: 'isApp',
          placeholder: '下订标识',
          itemList: this.createDictList('1004')
        }
      ];
    },
    isEdit(row) {
      if ((row.ORDER_STATUS === 40081005 || row.ORDER_STATUS === 40081001) && row.SPECIAL_STATUS !== 40091002 && row.ORDER_TYPE !== 40051001) {
        return false;
      } else {
        return true;
      }
    },
    isCommit(row) {
      if ((row.ORDER_STATUS === 40081005 || row.ORDER_STATUS === 40081001) && (row.ORDER_TYPE !== 40051005 || (row.ORDER_TYPE === 40051005 && row.SPECIAL_STATUS === 40091005))) {
        return false;
      } else {
        return true;
      }
    },
    isApproved(row) {
      if (row.SPECIAL_STATUS === 40091003 || row.SPECIAL_STATUS === 40091001 || row.SPECIAL_STATUS === 40091005 || row.SPECIAL_STATUS === 40091006) {
        return false;
      } else {
        return true;
      }
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val) {
      this.visible = val;
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
